//
//  TEImageLoaderViewController.m
//  TintExample
//
//  Created by Kevin Wu on 2/29/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TEImageLoaderViewController.h"
#import "TEImages.h"


@implementation TEImageLoaderViewController

- (void)dealloc {
    
    for (UIImageView *iv in _imageViews) {
        [[TTURLImageLoader sharedObject] cancelRequestingTarget:iv];
    }
    
    
    TTRelease(_imageURLs);
    TTRelease(_imagePaths);
    
    TTRelease(_imageView);
    
    TTRelease(_imageViews);
    TTRelease(_scrollView);
    [super dealloc];
}

#pragma mark - UIViewController

- (void)loadView {
    [super loadView];
    
    _imageView = [[UIImageView alloc] initWithFrame:TTRectMake(10, 110, 300, 300)];
    _imageView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_imageView];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:TTRectMake(10, 110, 300, 300)];
    _scrollView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_scrollView];
    
    [_imageView bringToFront];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSMutableArray *tmp = nil;
    
    tmp = [NSMutableArray arrayWithArray:TESmallImageURLArray()];
    [tmp insertObject:@"lskf" atIndex:3];
    _imageURLs = [[NSArray alloc] initWithArray:tmp];
    
    tmp = [NSMutableArray arrayWithArray:TESmallImagePathArray()];
    [tmp insertObject:@"lskf" atIndex:3];
    _imagePaths = [[NSArray alloc] initWithArray:tmp];
    
    NSMutableArray *array = [NSMutableArray array];
    for (int i=0; i<[_imageURLs count]; ++i) {
        UIImageView *iv = [[UIImageView alloc] init];
        iv.frame = CGRectMake(10, 10 + (280+10)*i, 280, 280);
        iv.backgroundColor = [UIColor darkGrayColor];
        [array addObject:iv];
        [_scrollView addSubview:iv];
    }
    _imageViews = [[NSArray alloc] initWithArray:array];
    
    
    [_scrollView resetContentSizeWithBottomPadding:10 rightPadding:0];
    
    [self addButtonWithTitle:@"Single ImageView" selector:@selector(style1:)];
    [self addButtonWithTitle:@"Multiple ImageView" selector:@selector(style2:)];
}


- (void)style1:(id)sender {
    [_imageView bringToFront];
    
//    static int i = 0;
//    NSString *path = [_imagePaths objectAtIndex:i];
//    ++i;
//    i = i%[_imageURLs count];
//    
//    
//    TTDiskImageLoader *loader = [TTDiskImageLoader loaderWithPath:path 
//                                                           target:_imageView 
//                                                          success:@selector(setImage:) 
//                                                          failure:@selector(setImage:) 
//                                                     failureImage:[UIImage imageNamed:@"placeholder_failure.png"]];
//    [loader startLoading];
    
    
    static int i = 0;
    NSString *address = [_imageURLs objectAtIndex:i];
    ++i;
    i = i%[_imageURLs count];
    
    
    [[TTURLImageLoader sharedObject] loadImageWithAddress:address 
                                     cacheTimeoutInterval:0 
                                                   target:_imageView 
                                                  success:@selector(setImage:) 
                                                  failure:@selector(setImage:) 
                                         placeholderImage:[UIImage imageNamed:@"placeholder_normal.png"] 
                                             failureImage:[UIImage imageNamed:@"placeholder_failure.png"]];
}

- (void)style2:(id)sender {
    [_scrollView bringToFront];
    
    static int i = 0;
    ++i;
    
    
    NSUInteger count = [_imageURLs count];
    for (int j=0; j<count; ++j) {
        NSString *address = [_imageURLs objectAtIndex:(i+j)%count];
        NSString *imageView = [_imageViews objectAtIndex:j];
        
        [[TTURLImageLoader sharedObject] loadImageWithAddress:address 
                                         cacheTimeoutInterval:0 
                                                       target:imageView 
                                                      success:@selector(setImage:) 
                                                      failure:@selector(setImage:) 
                                             placeholderImage:[UIImage imageNamed:@"placeholder_normal.png"] 
                                                 failureImage:[UIImage imageNamed:@"placeholder_failure.png"]];
    }
    
    
}

@end
